{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas import DataFrame,Series\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Pandas  PyTorch\n0        81      84      121\n1        54     102       12\n2         8      67       79\n3        84       3      120\n4        86     140      127\n..      ...     ...      ...\n145     116     125       36\n146      90      17       60\n147      99     134      124\n148      12     147        5\n149     124      28       81\n\n[150 rows x 3 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Pandas</th>\n      <th>PyTorch</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>81</td>\n      <td>84</td>\n      <td>121</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>54</td>\n      <td>102</td>\n      <td>12</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8</td>\n      <td>67</td>\n      <td>79</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>84</td>\n      <td>3</td>\n      <td>120</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>86</td>\n      <td>140</td>\n      <td>127</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>145</th>\n      <td>116</td>\n      <td>125</td>\n      <td>36</td>\n    </tr>\n    <tr>\n      <th>146</th>\n      <td>90</td>\n      <td>17</td>\n      <td>60</td>\n    </tr>\n    <tr>\n      <th>147</th>\n      <td>99</td>\n      <td>134</td>\n      <td>124</td>\n    </tr>\n    <tr>\n      <th>148</th>\n      <td>12</td>\n      <td>147</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>149</th>\n      <td>124</td>\n      <td>28</td>\n      <td>81</td>\n    </tr>\n  </tbody>\n</table>\n<p>150 rows × 3 columns</p>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,150,size=(150,3))\n",
    "df = pd.DataFrame(data=data, columns=['Python','Pandas','PyTorch'])\n",
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 81,  54,   8,  84,  86,  16,  90,  12,   6, 118,  89,   0, 117,\n        28, 106,  74,  84, 101, 127,  70,  70, 106, 112,  80,   7,  22,\n         9,  54, 111,  48, 126,  36,  10,  15,  60,  46, 119, 108,  80,\n        97,  65,  93,  51,  93,  81,  94, 109, 109,  18,  99,  28,  89,\n       134,  96,  99, 119,  18, 111,  45, 101, 138,  63,  63, 122,  35,\n        99, 126,  36, 126,   4, 135,   9, 119,  36,  44,  58,  68,  86,\n        24,  39, 135,  52, 143, 108, 133,  65, 123,  64,  13,  71,  55,\n       143,  41,  54,  78, 105,  27, 125,  26, 111,  24,  80, 113,  40,\n        81,   0, 131,  75,  18,  41, 104,  57,   6,  76, 135,  97, 103,\n        51, 100,   6,  45, 124,  38,  70,   3, 120,  40, 100,  22,  36,\n        13,  44,   4,  38,  33, 141,  75,  73, 109,  43, 133,  68,  60,\n        44,  89, 116,  90,  99,  12, 124])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Python\"].values"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "0       (71.5, 107.25]\n1        (35.75, 71.5]\n2      (-0.143, 35.75]\n3       (71.5, 107.25]\n4       (71.5, 107.25]\n            ...       \n145    (107.25, 143.0]\n146     (71.5, 107.25]\n147     (71.5, 107.25]\n148    (-0.143, 35.75]\n149    (107.25, 143.0]\nName: Python, Length: 150, dtype: category\nCategories (4, interval[float64, right]): [(-0.143, 35.75] < (35.75, 71.5] < (71.5, 107.25] < (107.25, 143.0]]"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.cut(df.Python,bins=4)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Pandas  PyTorch label\n0        60      84      121     B\n1        54     102       12     C\n2         8      67       79     D\n3        84       3      120     A\n4        86     140      127     A\n..      ...     ...      ...   ...\n145     116     125       36    A+\n146      90      17       60     A\n147      99     134      124     A\n148      12     147        5     D\n149     124      28       81    A+\n\n[150 rows x 4 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Pandas</th>\n      <th>PyTorch</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>60</td>\n      <td>84</td>\n      <td>121</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>54</td>\n      <td>102</td>\n      <td>12</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8</td>\n      <td>67</td>\n      <td>79</td>\n      <td>D</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>84</td>\n      <td>3</td>\n      <td>120</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>86</td>\n      <td>140</td>\n      <td>127</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>145</th>\n      <td>116</td>\n      <td>125</td>\n      <td>36</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>146</th>\n      <td>90</td>\n      <td>17</td>\n      <td>60</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>147</th>\n      <td>99</td>\n      <td>134</td>\n      <td>124</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>148</th>\n      <td>12</td>\n      <td>147</td>\n      <td>5</td>\n      <td>D</td>\n    </tr>\n    <tr>\n      <th>149</th>\n      <td>124</td>\n      <td>28</td>\n      <td>81</td>\n      <td>A+</td>\n    </tr>\n  </tbody>\n</table>\n<p>150 rows × 4 columns</p>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[0,\"Python\"] = 60\n",
    "df[\"label\"] = pd.cut(df.Python,bins=[0,30,60,80,100,np.inf],right=False,labels=[\"D\",\"C\",\"B\",\"A\",\"A+\"])\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "0      中\n1      中\n2      差\n3      良\n4      良\n      ..\n145    优\n146    良\n147    良\n148    差\n149    优\nName: Python, Length: 150, dtype: category\nCategories (4, object): ['差' < '中' < '良' < '优']"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.qcut(df.Python, # 分箱数据\n",
    "        q=4, # 4等份\n",
    "        labels=['差', '中', '良', '优']) # 分箱后分类标签\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}